<%@page import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection"%>
<%@ page import="java.util.*" language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@page import="java.text.SimpleDateFormat" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Purchase Voucher Creation </title>

<script language="javascript" type="text/javascript">

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f14.rdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in DD/MM/YYYY Format");
            document.f14.rdate.value="";
            document.f14.rdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
}

</script>

<script language="javascript" type="text/javascript">

function populateSupplierId(){
    var supname = document.getElementById('supname');

    /* selected value of dropdown */
    var selectedSupplierId = supname.options[supname.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('supcode').value = selectedSupplierId; 
}
</script>     

<script language="javascript" type="text/javascript">

function NetAmount()
{
    var p1 = document.getElementById("purprodquantity").value;
    var p2 = document.getElementById("purprodrate").value;
   
    var p3=parseInt(p1) * parseFloat(p2);
 
    document.getElementById("purnetamount").value = p3; 
}
    
</script>     

<script language="javascript" type="text/javascript">

function TaxAmount()
{
	var tax1 = document.getElementById("purnetamount").value;
	var tax2 = document.getElementById("purtaxcat").value;
	
	var tax3= (tax1 * tax2) /100;
	
	document.getElementById("purtaxamount").value=tax3;
}

</script>

<script language="javascript" type="text/javascript">

function TotalAmount()
{
	var t1 = document.getElementById("purnetamount").value;
	var t2 = document.getElementById("purtaxamount").value;
	var t3 = document.getElementById("purtransport").value;
	var t4 = document.getElementById("purothercosts").value;
	
	var total=parseFloat(t1)+parseFloat(t2)+parseFloat(t3)+parseFloat(t4);
	
	document.getElementById("purtotalamount").value = total;

}
	
</script>

</head>
<body>
<form name="f12" action="EditPurchaseVoucherServlet" >

<% 		String selectedaccnumber = (String) request.getAttribute("vnumber");


		DBConnection dbc=new DBConnection();	
		Connection con=dbc.getNewConnection();

		Statement st = null;
		ResultSet rs = null;
		String code2="";
		
		String p1="",p2="",p3="",p4="",p5="",p15="";
		int p6=0,p14=0;
		double p7=0.0,p8=0.0,p9=0.0,p10=0.0,p11=0.0,p12=0.0,p13=0.0;
	
		try
		{
			st=con.createStatement() ;
			rs=st.executeQuery("select * from PurchaseVoucher2 where pvinno='"+selectedaccnumber+"' "); %>
  
			<%  while(rs.next()) 
				{ 
					p1=rs.getString(1);
					p2=rs.getString(2);
					p3=rs.getString(3);
					p4=rs.getString(4);
					p5=rs.getString(5);
					p6=rs.getInt(6);
					p7=rs.getDouble(7);
					p8=rs.getDouble(8);
					p9=rs.getDouble(9);
					p10=rs.getDouble(10);
					p11=rs.getDouble(11);
					p12=rs.getDouble(12);
					p13=rs.getDouble(13);
					p14=rs.getInt(14);
					p15=rs.getString(15);
				}  
		 }
		catch(Exception e){} 		
%>


<table>
<tr> <td> Against Purchase order number </td>
	  <td> <input type="text" name="purordnum"/> </td>
</tr>

<tr>
	<td>Invoice number</td>  
	<td><input type="text" name="purInnum" value="<%=p1 %>"/></td>
	
	<% 	
	     SimpleDateFormat fromsql = new SimpleDateFormat("yyyy-MM-dd");
		 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		 String v2 =formatter.format(fromsql.parse(p2));

	%>
	
	<td>Date</td>	
	<td><input type="text" name="purdte" value="<%=v2 %>" /></td>
</tr>

<tr>
	<td>Name of Supplier</td>
	<td> <input type="text" name="supname" id="supname" value="<%=p3 %>"> </td>	
<%--			<%	 
				 int i=1;
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select scode,sname from SupplierMaster"); 
       		   %>
	
		<td> 
	   		<select name="supname" id="supname" onchange="populateSupplierId();" >
	   			<option> </option>
	  
       		 <%  while(rs.next()){ %>
            		<option value="<%=rs.getString(1) %>" ><%= rs.getString(2)%></option>
            		
            		<% } %>
      		</select>
     	</td>      		
     
 
          <%    }
		catch(Exception e){} %>
--%>		
    	<td><a href="SupplierMaster.jsp"> Create New Supplier </a></td>
		<%-- <td><input type="submit" name="purrefaccnames" value="Refresh account names"/> </td>  --%>
</tr>

<tr>
	<td>Supplier code</td>
	<td><input type="text" name="supcode" id="supcode" value="<%=p4 %>"/>
      
	
	<td>Supplier Invoice number</td>
	<td><input type="text" name="supinnum" /> </td>
	</tr>
	
<tr>
	<td>Product Name</td>
	<td><input type="text" name=purprodname value="<%=p5 %>"></td>
	
<%--			 <%	 
			try
			{
			   st=con.createStatement();
       		   rs=st.executeQuery("select stname from StockMaster"); %>
       		   
	<td> 
	  <select name="purprodname">
	  	<option> </option>
       		 <%  while(rs.next()){ %>
            		<option><%= rs.getString(1)%></option>
      
          <% } %>
       </select>
     </td>
  
	 <% }
		catch(Exception e){} %>
--%>

	<!-- <td><input type="submit" name="purnewstock" value="Create New Stock"/></td> -->
	
	<td><a href="StockMaster.jsp"> Create New Stock </a></td>
	<%--  <td><input type="submit" name="purrefitem" value="Refresh Item names"/></td>  --%>
	</tr>
	
<tr>
	<td>Product Quantity</td>
	<td><input type="text" name="purprodquantity" id="purprodquantity" value="<%=p6 %>"/></td>
	<td>Product Rate</td>
	<td><input type="text" name="purprodrate" id="purprodrate" value="<%=p7 %>" onblur="NetAmount();"/></td>
	</tr>
<tr> 
	<td>Purchase Net Amount</td>
	<td><input type="text" name="purnetamount" id="purnetamount" value="<%=p8 %>"/></td>
	<%-- <td><input type="button" name="purcalnetamount" id="purcalnetamount" value="Calculate Net Amount" onClick="NetAmount(); " /></td> --%> 
	
</tr>
<tr>
	<td>Taxation Category</td>
	<td> <input type=text name="purtaxcat" id="purtaxcat" value="<%=p9 %>" onblur="TaxAmount();">
<%-- 	<select name="purtaxcat" id="purtaxcat" onchange="TaxAmount((this.options[this.selectedIndex]));" >
		<option> </option>
		<option value="5.0">5.0</option>
		<option value="12.5">12.5</option>
	</select>
	--%>
	</td>
</tr>
	
<tr>
	<td>Purchase Tax Amount</td>
	<td><input type="text" name="purtaxamount" id="purtaxamount" value="<%=p10 %>" /></td>
	<%-- <td><input type="button" name="purcaltaxamount" id="purcaltaxamount" value="Calculate Tax Amount" onClick="TaxAmount();" /></td>  --%>
</tr>
	
<tr>
	<td>Purchase Transport Charges</td>
	<td> <input type="text" name="purtransport" id="purtransport" value="<%=p11 %>"/></td>
	<td>Purchase Other Costs</td>
	<td><input type="text" name="purothercosts" id="purothercosts" value="<%=p12 %>" onblur="TotalAmount();"/></td>
</tr>
	
<tr>
	<td>Purchase Total Amount</td>
	<td><input type="text" name="purtotalamount" id="purtotalamount" value="<%=p13 %>"/></td>
	<%--  <td><input type="button" name="purcaltotalamt" value="Calculate Total Amount" onClick="TotalAmount();" /></td>  --%>
</tr>
	
<tr>
	<td>Credit limit (No. of Days)</td>
	<td><input type="text" name="purcredlimit" value="<%=p14 %>"/></td>
</tr>

<tr>
	<td><input type="submit" name="enter1" value="Enter" /></td>
</tr>

</table>
</form>

<form action="PurchaseVoucherDeleteServlet">
 <input type="submit" value="Delete">
 <input type=hidden name="delete" value="<%=p1 %>">
</form>
</body>
</html>